package com.zzmyjjxy.view;

import com.zzmyjjxy.dao.SharedBikeDao;
import com.zzmyjjxy.entity.SharedBike;
import com.zzmyjjxy.entity.SharedBikeCompany;

import java.util.InputMismatchException;
import java.util.Scanner;

public class SharedBikeMgr {

    private SharedBikeDao dao = new SharedBikeDao();

    public void startMenu() {
        dao.init();
        System.out.println("欢迎使用迷你共享单车管理系统");
        Scanner sc = new Scanner(System.in);
        while (true) {
            try {
                printMenu();
                System.out.print("请选择：");
                int i = sc.nextInt();
                switch (i) {
                    case 1:
                        launchBike(sc);
                        showMenu(sc);
                        break;
                    case 2:
                        lookBike();
                        showMenu(sc);
                        break;
                    case 3:
                        deleteBike(sc);
                        showMenu(sc);
                        break;
                    case 4:
                        borrowBike(sc);
                        showMenu(sc);
                        break;
                    case 5:
                        returnBike(sc);
                        showMenu(sc);
                        break;
                    case 6:
                        rankOfBike();
                        showMenu(sc);
                        break;
                    case 0:
                        System.exit(1);
                        break;
                    default:
                        System.err.println("输入的数字必须是1-7");
                        break;
                }
            } catch (InputMismatchException e) {
                System.err.println("输入有误,格式不匹配");
                sc.next();
                showMenu(sc);
            } catch (Exception e) {
                System.err.println("输入有误,错误原因：" + e.getMessage());
                showMenu(sc);
            }
        }
    }

    private void showMenu(Scanner sc) {
        System.out.println("输入任意字符，然后enter回到主菜单");
        sc.next();
    }

    private void printMenu() {
        System.out.println("***********************************************************************");
        System.out.println("           1，投放Bike");
        System.out.println("           2，查看Bike");
        System.out.println("           3，删除Bike");
        System.out.println("           4，借出Bike");
        System.out.println("           5，归还Bike");
        System.out.println("           6，Bike排行榜");
        System.out.println("           0，退      出");
        System.out.println("***********************************************************************");
    }

    private void launchBike(Scanner sc) {
        System.out.println("------------> 1. 投放Bike");
        SharedBikeCompany company = getSharedBikeCompany(sc, false);
        System.out.print("请输入要投放的数量:");
        int num = sc.nextInt();
        for (int j = 0; j < num; j++) {
            SharedBike bike = new SharedBike();
            bike.setStatus(0);
            bike.setBid(dao.getMaxBikeNo(company));
            bike.setBname(company.getBikeCompanyName());
            dao.addBike(company, bike);
        }
        company.setSum(company.getSum() + num);
        System.out.println("投放成功");
    }

    public void lookBike() {
        System.out.println("------------> 2. 查看Bike");
        SharedBikeCompany[] companies = dao.getCompanies();
        for (SharedBikeCompany company : companies) {
            printCompany(company);
            if (company.getSum() > 0) {
                printBikes(company);
            }
        }
    }

    private void deleteBike(Scanner sc) {
        System.out.println("------------> 3. 删除Bike");
        SharedBikeCompany company = getSharedBikeCompany(sc, true);
        System.out.print("请输入要删除的bike的id：");
        int bikeId = sc.nextInt();
        dao.deleteBike(company, bikeId);
        System.out.println("删除成功");
    }

    private void borrowBike(Scanner sc) {
        System.out.println("------------> 4. 借出Bike");
        borrowOrReturnBike(sc, 0);
    }

    private void returnBike(Scanner sc) {
        System.out.println("------------> 5. 归还Bike");
        borrowOrReturnBike(sc, 1);
    }

    private void borrowOrReturnBike(Scanner sc, int flag) {
        SharedBikeCompany company = getSharedBikeCompany(sc, true);
        System.out.print("请输入要" + (flag == 0 ? "借出" : "归还") + "的bike的id：");
        int bikeId = sc.nextInt();
        System.out.println("请输入要" + (flag == 0 ? "借出" : "归还") + "的时间（按照格式2023-12-20 12:12:12）：");
        sc.nextLine();
        String timeStr = sc.nextLine();//null
        SharedBike bike = dao.borrowOrReturnBike(flag, company, bikeId, timeStr);
        if (bike != null) {
            System.out.println((flag == 0 ? "借出" : "归还") + "成功");
        } else {
            throw new RuntimeException("没找到这个id的单车");
        }
    }

    private void rankOfBike() {
        System.out.println("------------> 6. 排行榜");
        SharedBikeCompany[] companies = dao.rankOfBike();
        System.out.println("公司名称   借车次数");
        for (SharedBikeCompany company : companies) {
            System.out.println(company.getBikeCompanyName() +
                    "     " + company.getCount());
        }
    }

    private void printCompany(SharedBikeCompany company) {
        System.out.println("+----------+----------+---------- +-----------------+--------------+---------------+------------+--------------+");
        System.out.println("|  公司序号  |  公司名称 | 公司单车数量 |   公司单车借出次数    |");
        System.out.println("+-----------+---------+------------+---------------------+");
        System.out.println("|     " + company.getBikeCompanyId() + "    |  "
                + company.getBikeCompanyName() + "  |     " + company.getSum() + "     |      " + company.getCount() + "             |");
        System.out.println("+-----------+---------+------------+---------------------+");
    }

    private void printCompanyList(SharedBikeCompany[] companies) {
        System.out.println("有以下单车品牌可供选择：");
        for (SharedBikeCompany company : companies) {
            System.out.println("|     " + company.getBikeCompanyId() + "    |  "
                    + company.getBikeCompanyName() + " |");
        }
    }

    private SharedBikeCompany getSharedBikeCompany(Scanner sc, boolean flag) {
        SharedBikeCompany[] companies = dao.getCompanies();
        printCompanyList(companies);
        System.out.print("请输入要操作的品牌的id：");
        int i = sc.nextInt();
        SharedBikeCompany company = dao.getCompanyByNo(i);
        if (company == null) {
            throw new RuntimeException("没找到该公司，请输入正确的id");
        }
        if (company.getSum() > 0 && flag) {
            printBikes(company);
        }
        return company;
    }

    private void printBikes(SharedBikeCompany company) {
        System.out.println("      |   单车序号   |    单车品牌   |    单车状态    |    借出时间   |");
        for (SharedBike bike : company.getSharedBikes()) {
            if (bike == null) {
                continue;
            }
            String status = bike.getStatus() == 0 ? "可借" : "不可借";
            String borrowTime = bike.getBorrowTime() != null ? bike.getBorrowTime() : "";
            System.out.println("      |     " + bike.getBid() + "      |    " + bike.getBname() + "   |    " + status + "        |    " + borrowTime + "     |");
        }
    }

}
